//
//  RegisterViewController.m
//  Car
//
//  Created by apple on 15/9/22.
//  Copyright (c) 2015年 朱鸿儒. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegisterView.h"

#import <SMS_SDK/SMS_SDK.h>



@interface RegisterViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) RegisterView *registerView;



@end

@implementation RegisterViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    [self setUpRegisterPage];
    
    self.registerView.userPhoneNumber.delegate = self;
    self.registerView.userPhoneNumber.tag = 91101;
    
    self.registerView.userPassword.delegate = self;
    self.registerView.userPassword.tag = 91103;
    
    self.registerView.userPasswordAgain.delegate = self;
    self.registerView.userPasswordAgain.tag = 91104;
    
    self.registerView.userName.delegate = self;
    self.registerView.userName.tag = 91102;
    
    self.registerView.identifyWord.delegate = self;
    self.registerView.identifyWord.tag = 91105;

    
    
    [self registerPageControl];
    
}

- (void)setUpRegisterPage {
    
    self.registerView = [[RegisterView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    
    [self.registerView.actionGetIdentifyWord addTarget:self action:@selector(actionClick) forControlEvents:UIControlEventTouchUpInside];
    [self.registerView.registerButton addTarget:self action:@selector(ActionClickToRegister) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.view addSubview:self.registerView];

    
}


#pragma mark ---   发送验证码

- (void)actionClick {
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    if ([user objectForKey:self.registerView.userPhoneNumber.text] != nil) {
        NSLog(@"电话号码重复");
    }
    else {
    
    [SMS_SDK getVerificationCodeBySMSWithPhone:self.registerView.userPhoneNumber.text
                                          zone:@"86"
                                        result:^(SMS_SDKError *error)
     {
         if (!error)
         {
             UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"验证码发送成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
             [alert show];
             
         }
         else
         {
             UIAlertView* alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"无法发送验证码", nil)
                                                             message:@"请输入正确的手机号"
                            
                                                            delegate:self
                                                   cancelButtonTitle:NSLocalizedString(@"sure", nil)
                                                   otherButtonTitles:nil, nil];
             [alert show];
         }
         
     }];

    }
    
    

}


#pragma mark ---   注册按钮

- (void)ActionClickToRegister {
    
    //保存输入框中注册时填写的信息
    NSString *phoneNumber = self.registerView.userPhoneNumber.text;
    NSString *password1 = self.registerView.userPassword.text;
    NSString *password2 = self.registerView.userPasswordAgain.text;
    NSString *userName = self.registerView.userName.text;
    NSString *identifyWord = self.registerView.identifyWord.text;
    
    
    
    if (phoneNumber.length == 0) {
        
        //创建提醒视图告之用户输入的信息不完整或者输入错误
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确手机号" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
        
    }
    else {
        
        if (password1.length == 0) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入密码(6-16位)" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
            
        }
        else {
            
            if (password1.length > 16 || password1.length < 6) {
                
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入密码(6-16位)" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                [alert show];

            }
            else {
                
                if (![password2 isEqualToString:password1]) {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"密码验证错误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                    [alert show];
                    
                }
                else {
                    
                    if (userName.length == 0) {
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"帮自己取一个名字吧" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                        [alert show];
                        
                    }
                    else {
                        
                        //  保存注册的用户信息
                        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                        
                        if ([user objectForKey:userName] != nil) {
                            NSLog(@"用户名重复");
                        }
                        else {

                        
                        [SMS_SDK commitVerifyCode:self.registerView.identifyWord.text result:^(enum SMS_ResponseState state) {
                            
                            if (1 == state)
                            {
                                                               

                                
                                //  保存注册的用户信息
                                NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                                
                                NSArray *userArray = [NSArray arrayWithObjects:phoneNumber, password1, password2, userName, identifyWord, nil];
                                
                                [user setObject:userArray forKey:@"tm"];
                                
                                //  同步信息到本地 实现数据持久化
                                [user synchronize];
                                
                                UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"注册成功" message:@"欢迎加入车行家" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                                [view show];
                                
                                
                                [self.navigationController popViewControllerAnimated:YES];
                            }
                            else if(0 == state)
                            {
                                
                                UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                                                message:@"验证失败"
                                                                               delegate:self
                                                                      cancelButtonTitle:@"确定"
                                                                      otherButtonTitles:nil, nil];
                                [alert show];
                            }
                        }];
                    }

                    }
                }
            }
        }
    }
}



#pragma mark ---   回收键盘   ---

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    [self.registerView.userPassword resignFirstResponder];
    [self.registerView.userName resignFirstResponder];
    [self.registerView.userPasswordAgain resignFirstResponder];
    [self.registerView.identifyWord resignFirstResponder];
    [self.registerView.userPhoneNumber resignFirstResponder];
    
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField.tag == 91101) {
        textField = (UITextField *)[self.view viewWithTag:91102];
        return [textField becomeFirstResponder];
    }
    else if (textField.tag == 91102) {
        textField = (UITextField *)[self.view viewWithTag:91103];
        return [textField becomeFirstResponder];
        
    }
    else if (textField.tag == 91103) {
        textField = (UITextField *)[self.view viewWithTag:91104];
        return [textField becomeFirstResponder];
    }
    
    else if (textField.tag == 91104) {
        textField = (UITextField *)[self.view viewWithTag:91105];
        return [textField becomeFirstResponder];
    }
    else {
        return [textField resignFirstResponder];
    }

    
}





#pragma mark ---   navicationBar   ---

- (void)registerPageControl {
    
    self.navigationItem.title = @"新用户";
    
    
    
}




@end
